﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;

public partial class Url : System.Web.UI.Page
{

    public int AddAndUpdateSign = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminName"] == null)
            Response.Redirect("login.aspx");

        if (!Page.IsPostBack)
        {
           
            if (Request.QueryString["domainid"] != null)
            {
                int domainId = int.Parse(Request.QueryString["domainid"]);

                this.ddlGroup.DataSource = BLL.BUrlGroup.GetMyList();
                string[] keys ={ "UrlID" };
                gdvList.DataKeyNames = keys;
                this.ddlGroup.DataBind();

                DAL.UrlDALs urldals = new DAL.UrlDALs();
                this.gdvList.DataSource = urldals.GetUrlByDomainId(domainId);
                this.gdvList.DataBind();

            }
            else
            {
                this.ddlGroup.DataSource = BLL.BUrlGroup.GetMyList();
                string[] keys ={ "UrlID" };
                gdvList.DataKeyNames = keys;
                this.ddlGroup.DataBind();

                this.gdvList.DataSource = BLL.BUrl.GetMyList(0, string.Empty);
                this.gdvList.DataBind();
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        Entities.Url url = new Entities.Url();
        url.Url = txtUrls.Text.Trim();
        url.SendMessage = txtMsg.Text.Trim();
        url.IsSendEmail = cbSendMail.Checked;
        url.IsSendSMS = cbSendSMS.Checked;
        url.Time = this.txtFrom.Text + "-" + this.txtTo.Text;
        if (cbSendSMS.Checked && string.IsNullOrEmpty(this.txtSmsSet.Text.Trim()))
        {
 Response.Write("<script>alert('请设置短信发送规则')</script>");
            return;
        }
        else
        {
             url.F2 = (this.txtMailSet.Text.Trim());
        }

        if (cbSendMail.Checked && string.IsNullOrEmpty(this.txtMailSet.Text.Trim()))
        {
              Response.Write("<script>alert('请设置邮件发送规则')</script>");
            return;
           
        }
        else
        {
           url.F1 = (this.txtSmsSet.Text.Trim());
        }
        if (!string.IsNullOrEmpty(this.txtCheckConfig.Text.Trim()))
            url.F3 = Convert.ToInt16(this.txtCheckConfig.Text);
        else
        {
            Response.Write("<script>alert('请设置监测时间周期规则')</script>");
            return;
        }
        Entities.UrlGroup urlgroup = BLL.BUrlGroup.GetById(int.Parse(ddlGroup.SelectedValue));
        url.UrlGroup = urlgroup.UrlGroup;

        if (this.Button1.Text == "修改")
        {
            url.UrlID = Convert.ToInt16(ViewState["UrlID"]);
            BLL.BUrl.Update(url);
        }
        else
        {
            BLL.BUrl.Insert(url);
        }

        //CheckUrl url = new CheckUrl();
        //url.Query = this.txtQuery.Text;
        //url.Url = txtUrls.Text;
        //url.SendMessage = txtMessage.Text;
        //url.ID = System.Guid.NewGuid().ToString();

        //Provider<CheckDomain> domainPro = new Provider<CheckDomain>();
        //ObjectSet<CheckDomain> domainObj = domainPro.List();
        //foreach (CheckDomain item in domainObj)
        //{
        //    ///找到选中的域
        //    if (item.ID == this.ddlList.SelectedValue)
        //    {
        //        item.Urls.Add(url);
        //        continue;
        //    }
        //}
        //domainPro.Save(domainObj);
        ////ULR独立保存

        //Provider<CheckUrl> urlPro = new Provider<CheckUrl>();
        //urlPro.Insert(url);
        Response.Redirect("Url.aspx");
    }
    protected void gdvList_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(gdvList.DataKeys[e.NewEditIndex].Value);
        Entities.Url url = new Entities.Url();
        url = BLL.BUrl.Load(id);
        if (url != null)
        {
            if (!string.IsNullOrEmpty(url.F1))
                this.txtSmsSet.Text = url.F1;
            else
                this.txtSmsSet.Text = "1,15";
            if (!string.IsNullOrEmpty(url.F2))
                this.txtMailSet.Text = url.F2;
            else
                this.txtMailSet.Text = "3^N";
            if (url.F3!=null && url.F3>0)
                this.txtCheckConfig.Text = url.F3.ToString();
            else
                this.txtCheckConfig.Text = "5";


            txtUrls.Text = url.Url;
            txtMsg.Text = url.SendMessage;
            if (url.IsSendEmail)
            {
                cbSendMail.Checked = true;
                this.txtMailSet.Enabled = true;
            }
            if (url.IsSendSMS)
            {
                cbSendSMS.Checked = true;
                this.txtSmsSet.Enabled = true;
                
            }

            this.ddlGroup.DataSource = BLL.BUrlGroup.GetMyList();
            this.ddlGroup.DataBind();

            foreach (ListItem item in ddlGroup.Items)
            {
                if (url.UrlGroup == item.Text.Trim())
                {
                    item.Selected = true;
                }
            }
            this.Button1.Text = "修改";
            ViewState["UrlID"] = url.UrlID;
            if (!string.IsNullOrEmpty(url.Time))
            {
                string[] time = url.Time.Split("-".ToCharArray());
                if (time.Length == 2)
                {
                    txtFrom.Text = time[0];
                    txtTo.Text = time[1];
                }
            }
        }

       

    }

    protected void gdvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(gdvList.DataKeys[e.RowIndex].Value);
        Entities.Url url = BLL.BUrl.Load(id);
        DAL.UrlDALs urldals = new DAL.UrlDALs();
        urldals.DeleteUrls(id);
        Response.Redirect("Url.aspx");
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(this.HidUrlsId.Value);
        Entities.Url url = BLL.BUrl.Load(id);
        url.Url = txtUrls.Text.Trim();
        url.SendMessage = txtMsg.Text.Trim();
        url.IsSendEmail = cbSendMail.Checked;
        url.IsSendSMS = cbSendSMS.Checked;
        Entities.UrlGroup urlgroup = BLL.BUrlGroup.GetById(int.Parse(ddlGroup.SelectedValue));
        url.UrlGroup = urlgroup.UrlGroup;
        BLL.BUrl.Update(url);

        Response.Redirect("Url.aspx");
    }
    protected void gdvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gdvList.PageIndex = e.NewPageIndex;
        this.ddlGroup.DataSource = BLL.BUrlGroup.GetMyList();
        string[] keys ={ "UrlID" };
        gdvList.DataKeyNames = keys;
        this.ddlGroup.DataBind();

        this.gdvList.DataSource = BLL.BUrl.GetMyList(0, string.Empty);
        this.gdvList.DataBind();
    }
    protected void cbSendSMS_CheckedChanged(object sender, EventArgs e)
    {
        if (this.cbSendSMS.Checked)
            this.txtSmsSet.Enabled = true;
        else
            this.txtSmsSet.Enabled = false;
    }
    protected void cbSendMail_CheckedChanged(object sender, EventArgs e)
    {
        if (this.cbSendMail.Checked)
        {
            this.txtMailSet.Enabled = true;
        }
        else
        {
            this.txtMailSet.Enabled = false;
        }
    }
    
}
